//
//  HMOriginalStatusView.swift
//  MyWeibo
//
//  Created by apple on 16/4/21.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit
import SnapKit

class HMOriginalStatusView: UIView {
    
    //取到底部约束 设置属性
    private var bottomCons : Constraint?
    
    
    // 要拿到数据，才能让当前视图身上的控件去显示数据
    var statusViewModel: HMStatusViewModel?{
        
        didSet{
            
            //设置头像
            headerImage.sd_setImageWithURL(NSURL(string: statusViewModel?.status?.user?.profile_image_url ?? ""), placeholderImage:UIImage(named: "avatar_default_small"))
            //设置名字
            nameLabel.text = statusViewModel?.status?.user?.name
            // 设置会员图标
            memberImageView.image = statusViewModel?.memberImage
            // TODO : 时间与来源 (会重新设置)
            timeLabel.text = statusViewModel?.createAtText
             sourceLabel.text = statusViewModel?.sourceText
            
            //设置微博正文内容
            contentLabel.text = statusViewModel?.status?.text
            
            //设置配图  首先卸载底部约束
            
            bottomCons?.uninstall()
            //如果有配图
            if let pic_urls = statusViewModel?.status?.pic_urls where pic_urls.count > 0 {
            
                //元组返回赋值  设置配图大小和宽高
                pictureView.dataInfo = (pic_urls , statusViewModel!.originalPictureViewSize)
                
                //显示配图
                pictureView.hidden = false
                
                //更新配图约束
                self.snp_makeConstraints(closure: { (make) -> Void in
                    
                    bottomCons = make.bottom.equalTo(pictureView).offset(HMStatusCellMargin).constraint
                })
                
            }else{
                //如果没有
                pictureView.hidden = true
                //更新约束到contantlabel
                self.snp_makeConstraints(closure: { (make) -> Void in
                    
                   bottomCons = make.bottom.equalTo(contentLabel).offset(HMStatusCellMargin).constraint
                    
                })
            }
        }
    }
    
    //初始化控件frame
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置控件位置
    func setupUI(){
        
        //设置view的背景颜色
//        backgroundColor = UIColor.whiteColor()
        
        //添加控件
        addSubview(headerImage)
        addSubview(nameLabel)
        addSubview(memberImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(verifiedImageView)
        addSubview(contentLabel)
        
        //添加配图控件
        addSubview(pictureView)
        
        
        headerImage.layer.cornerRadius = 25
        headerImage.layer.masksToBounds = true
        
        //设置约束
        
        //头像
        headerImage.snp_makeConstraints { (make) -> Void in
            make.top.leading.equalTo(HMStatusCellMargin)
            make.size.equalTo(CGSize(width: HMStatusCellHeadImgWH, height: HMStatusCellHeadImgWH))
            
        }
        //昵称
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headerImage)
            make.leading.equalTo(headerImage.snp_trailing).offset(HMStatusCellMargin)
            
        }
        
        // 会员
        memberImageView.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(nameLabel)
            make.leading.equalTo(nameLabel.snp_trailing).offset(HMStatusCellMargin)
        }

        
        //时间
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(headerImage)
            make.leading.equalTo(nameLabel)
        }

        //来源
        sourceLabel.snp_makeConstraints { (make) -> Void in
            
        make.leading.equalTo(timeLabel.snp_trailing).offset(HMStatusCellMargin)
            make.top.equalTo(timeLabel)
            
        }
        //认证
        verifiedImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headerImage.snp_trailing).offset(-2)
            make.centerY.equalTo(headerImage.snp_bottom).offset(-2)
        }
        //正文内容
        contentLabel.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(headerImage.snp_bottom).offset(HMStatusCellMargin)
            make.leading.equalTo(headerImage)
            
        }
        //配图约束
        pictureView.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(contentLabel)
            
            make.top.equalTo(contentLabel.snp_bottom).offset(HMStatusCellMargin)
            
            //测试宽高
            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        
        //自身约束
        self.snp_makeConstraints { (make) -> Void in
            
           bottomCons = make.bottom.equalTo(pictureView.snp_bottom).offset(HMStatusCellMargin).constraint
        }
        
    }
    
    
    //懒加载头像
    private lazy var headerImage : UIImageView = UIImageView(image: UIImage(named: "avatar_default_small"))
    
    // 昵称的label
    private lazy var nameLabel: UILabel = {
        let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: HMStatusCellNameFontSize)
        label.text = "哈哈"
        return label
    }()
    // 会员图片
    private lazy var memberImageView: UIImageView = UIImageView(image: UIImage(named:"common_icon_membership"))
    
    // 时间
    private lazy var timeLabel: UILabel = {
        let label = UILabel(textColor: UIColor.orangeColor(), fontSize: HMStatusCellTimeFontSize)
        label.text = "刚刚"
        return label
    }()
    
    // 来源
    private lazy var sourceLabel: UILabel = {
        let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: HMStatusCellTimeFontSize)
        label.text = "来自 黑马微博"
        return label
    }()
    
    // 认证
    private lazy var verifiedImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    
    //懒加载微博显示内容
    
    private lazy var contentLabel : UILabel = {
        let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: HMStatusCellContentFontSize, preferredMaxLayoutWidth: SCREENW - 2 * HMStatusCellMargin)
        
        return label
    }()
    
    //懒加载原创视图控件 先给一个默认大小和布局 默认零大小和初始化布局
    private lazy var pictureView:HMStatusPictureView = HMStatusPictureView(frame: CGRectZero, collectionViewLayout: UICollectionViewFlowLayout())
    
    
}
